package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.List;
import java.util.Objects;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowNodeParticipantSettingVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNodeParticipantSettingRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNodeParticipantSetting;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNodeParticipantSettingExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowNodeParticipantSettingDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author lilh
 * @date 2021/1/6 下午3:59
 */
@Repository
public class FlowNodeParticipantSettingRepositoryImpl implements FlowNodeParticipantSettingRepository {

    @Autowired
    private FlowNodeParticipantSettingDAO nodeParticipantSettingDAO;

    @Override
    public int insert(FlowNodeParticipantSettingVo record) {
        FlowNodeParticipantSetting entity = new FlowNodeParticipantSetting();
        BeanUtils.copyProperties(record, entity);
        return nodeParticipantSettingDAO.insertSelective(entity);
    }

    @Override
    public boolean deleteSetting(String modelKey, List<String> nodeCodes) {
        FlowNodeParticipantSettingExample query = new FlowNodeParticipantSettingExample();
        query.createCriteria().andModelKeyEqualTo(modelKey).andStateEqualTo("Y").andNodeCodeIn(nodeCodes);
        FlowNodeParticipantSetting entity = new FlowNodeParticipantSetting();
        entity.setState("N");
        return nodeParticipantSettingDAO.updateByExampleSelective(entity, query) > 0;
    }

    @Override
    public FlowNodeParticipantSettingVo getNodeSetting(String modelKey, String nodeCode) {
        FlowNodeParticipantSettingExample query = new FlowNodeParticipantSettingExample();
        query.createCriteria().andModelKeyEqualTo(modelKey).andNodeCodeEqualTo(nodeCode)
                .andStateEqualTo("Y");
        List<FlowNodeParticipantSetting> settings = nodeParticipantSettingDAO.selectByExample(query);
        if (CollectionUtils.isNotEmpty(settings) && Objects.nonNull(settings.get(0))) {
            FlowNodeParticipantSettingVo result = new FlowNodeParticipantSettingVo();
            BeanUtils.copyProperties(settings.get(0), result);
            return result;
        }
        return null;
    }
}
